package com.xk.androiddemo;

import android.content.Intent;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;


import com.xk.androiddemo.base.BaseActivityPortrait;
import com.xk.androiddemo.base.CommonAdapter;
import com.xk.androiddemo.database.AppDatabase;
import com.xk.androiddemo.database.UserBean;
import com.xk.androiddemo.databinding.ActivitySwitchAccountsBinding;
import com.xk.androiddemo.databinding.ItemAccontSwitchBinding;

import java.security.SignatureException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class SwitchAccountsActivity extends BaseActivityPortrait<ActivitySwitchAccountsBinding> {

    private CommonAdapter<ItemAccontSwitchBinding, UserBean> commonAdapter;
    private List<UserBean> list;
    @Override
    protected void initData() {
        commonAdapter =new CommonAdapter<ItemAccontSwitchBinding, UserBean>(new ArrayList<>()) {
            @Override
            protected void show(ItemAccontSwitchBinding holder, int position, UserBean userData) {
                holder.tvUserTitle.setText("用户名:"+userData.getName());

                // 设置电话号码
                holder.tvUserPhone.setText("手机号:"+userData.getPhone());
                holder.main.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        App.userBean = userData;
                        Intent intent = new Intent(SwitchAccountsActivity.this, MainActivity.class);
                        startActivity(intent);
                        finish();
                    }
                });
            }
        };
        bind.rv.setAdapter(commonAdapter);
        bind.ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent  intent =new Intent(SwitchAccountsActivity.this, LoginActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }

    @Override
    protected void onclick() {
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        executorService.execute(new Runnable() {
            @Override
            public void run() {
                list = AppDatabase.getDatabase(SwitchAccountsActivity.this).UserDao().getAllUsers();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        commonAdapter.setData(list);
                    }
                });
            }
        });
    }

    @Override
    protected void initPortraitView() {
        bind.rv.setLayoutManager(new LinearLayoutManager(this));
    }
}